layui.use(['table', 'jquery', 'tree'], function () {
    const table = layui.table,
        $ = layui.jquery,
        tree = layui.tree;

    let result = commonAjax(AUTH_SERVER_URL+'/permission/findMenuTree', {type: 2}, "get");
    let data = result.data.list;

    let pid = null;

    //加载树
    tree.render({
        elem: '#tree'
        , data: data
        , showCheckbox: false
        , id: 'pmsTree'
        , isJump: false
        , click: function (obj) {
            let data = obj.data;
            pid = data.id;
            findAll();
        }
    });

    //默认加载时执行
    findAll();

    function findAll() {

        let name = $('#name').val();
        let code = $("#code").val();
        let params = {
            name: name,
            code: code,
            pid: pid,
            type: 2
        };

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 60, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'code', width: '15%', title: '权限编号', minWidth: 160}
            , {field: 'pid', width: 60, title: '父ID', minWidth: 160}
            , {
                field: 'type', width: 80, title: '类型', templet: e => {
                    return getPermissionStatus(e.type);
                }
            }
            , {field: 'name', width: 140, title: '权限名'}
            , {field: 'value', width: '18%', title: '权限值', minWidth: 160}
            , {field: 'uri', width: '18%', title: '方法路径', minWidth: 200}
            , {field: 'createTime', title: '创建时间', width:  '15%'}
            , {fixed: 'right', width: 80, align: 'center', toolbar: '#operating'}
        ]];


        //发送请求
        table.render({
            elem: '#dataList'
            , url: AUTH_SERVER_URL + "/permission/findAll"
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 20, 30, 50]
            , limit: 20
            , title: '功能数据表'
            , cols: cols
        });
    }

    //监听头工具栏事件
    table.on('toolbar(dataList)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'add':
                if (pid == null) {
                    layer.msg("请选择分类");
                } else {
                    addPmsMethod();
                }
                break;
            case 'batchDel':
                if (data.length === 0) {
                    layer.msg('请至少选择一行');
                } else {
                    let ids = [];
                    for (let i = 0; i < data.length; i++) {
                        ids += checkStatus.data[i].id + ',';
                    }
                    //去掉最后一个逗号
                    ids = ids.substring(0, ids.lastIndexOf(','));
                    layer.confirm('此操作不可逆，确定删除？', function (index) {
                        layer.close(index);
                        batchDel(ids.toString())
                    });
                }
                break;
        }
    });

    //监听行工具事件
    table.on('tool(dataList)', function (obj) {
        const data = obj.data //获得当前行数据
            , layEvent = obj.event;
        if (layEvent === 'edit') {
            updatePmsMethod(data.id);
        }
    });


    /**
     * 添加功能
     */
    function addPmsMethod() {
        sessionStorage.setItem("methodPid", pid);
        commonLayerOpen('新增功能', 'pmsMethodAdd.html');
    }

    /**
     * 批量删除
     */
    function batchDel(ids) {

        let result = commonBatchAjax(AUTH_SERVER_URL+'/permission/deletes', {ids: ids}, 'post');
        if (result.code === 200) {
            layerMsgDeleteSuccess();
            findAll();
        } else {
            layerMsgDeleteFail();
        }
    }

    /**
     * 修改
     * @param id
     */
    function updatePmsMethod(id) {
        sessionStorage.setItem("pmsMethodId", id);
        commonLayerOpen('功能详情', 'pmsMethodDetails.html');
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll()
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#code').val('');
        $('#name').val('');
    });
});